#! /usr/bin/python

import os
import pdfrw


INVOICE_TEMPLATE_PATH = 'newtemplate.pdf'
INVOICE_OUTPUT_PATH = 'invoice.pdf'



def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict, which="pacg"):
    if which == "pacg":
        pacg_write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict)
    elif which == "apostl":
        apostl_write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict)

def apostl_write_fillable_pdf(input_pdf_path, output_pdf_path, adata_dict):
    ANNOT_KEY = '/Annots'
    ANNOT_FIELD_KEY = '/T'
    ANNOT_VAL_KEY = '/V'
    ANNOT_RECT_KEY = '/Rect'
    SUBTYPE_KEY = '/Subtype'
    WIDGET_SUBTYPE_KEY = '/Widget'
    template_pdf = pdfrw.PdfReader(input_pdf_path)
    template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true')))
    #annotations = template_pdf.pages[0][ANNOT_KEY]
    # only one page
    annotations =  template_pdf.Root.Pages.Kids[0].Annots
    for annotation in annotations:
        if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
            if annotation[ANNOT_FIELD_KEY]:
                key = annotation[ANNOT_FIELD_KEY][1:-1]
                #print(key)
                if key in adata_dict.keys():
                    annotation.update(
                        pdfrw.PdfDict(AP='{}'.format(adata_dict[key]), V='{}'.format(adata_dict[key]))
                    )


    annotations =  template_pdf.Root.Pages.Kids[2].Annots
    for annotation in annotations:
        if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
            if annotation[ANNOT_FIELD_KEY]:
                key = annotation[ANNOT_FIELD_KEY][1:-1]
                #print(key)
                if key in adata_dict.keys():
                    annotation.update(
                        pdfrw.PdfDict(AP='{}'.format(adata_dict[key]), V='{}'.format(adata_dict[key]))
                    )
    pdfrw.PdfWriter().write(output_pdf_path, template_pdf)


adata_dict = {
   'LAST NAME': 'Xie',  #first name
   'FIRST NAME': 'Changcai',  #second name
   'DATE OF BIRTH MMDDYYYY': '08/19/1983',
   'MED RECPATIENT IDENTIFIER': 'E932033231',  #birth
   'ETHNICITY': 'ASIA',  #street
   'ADDRESS': 'walnut creek',  #city
   'CITY': 'Berkely',  #zip
   'STATEPROVINCE': 'CA',  #county
   'POSTAL CODE':'053400',
   'COUNTRY':'USA',
   'PHONE': '18910158363', #phone
   'EMAIL': 'xiechc@gmail.com',  #email
   'Collection Date': '15/22/2020',  #report date
   'Date MMDDYYYY': '17/11/2020',
   'Date11_es_:signer:date': '29/11/2020',
   'Text10':'Changcai Xie'
}


def pacg_write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict):
    ANNOT_KEY = '/Annots'
    ANNOT_FIELD_KEY = '/T'
    ANNOT_VAL_KEY = '/V'
    ANNOT_RECT_KEY = '/Rect'
    SUBTYPE_KEY = '/Subtype'
    WIDGET_SUBTYPE_KEY = '/Widget'
    template_pdf = pdfrw.PdfReader(input_pdf_path)
    template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true'))) 
    #annotations = template_pdf.pages[0][ANNOT_KEY]
    # only one page
    annotations =  template_pdf.Root.Pages.Kids[0].Annots
    for annotation in annotations:
        if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
            if annotation[ANNOT_FIELD_KEY]:
                key = annotation[ANNOT_FIELD_KEY][1:-1]
                #print(key)
                if key in data_dict.keys():
                    annotation.update(
                        pdfrw.PdfDict(AP='{}'.format(data_dict[key]), V='{}'.format(data_dict[key]))
                    )
    pdfrw.PdfWriter().write(output_pdf_path, template_pdf)


data_dict = {
   'Text1': 'aostata',  #first name
   'Text2': 'bostata',  #second name
   'Text3': 'costata',  #birth
   'Text4': 'dostata',  #street
   'Text5': 'eostata',  #city 
   'Text6': 'fostata',  #zip
   'Text7': 'gostata',  #county
   'Text8': 'hostata',  #phone
   'Text9': 'iostata',  #email
   'Text10': 'jostata', #licence
   'Text11': 'kostata', #state/country
   'Text12': '10/22/2020',  #sampe receive date
   'Text13': '15/22/2020',  #report date

   'Text16': 'mm',
   'Text18': 'dd',
   'Text19': 'yy',
   'Text20': 'mm/dd/yy',
}

if __name__ == '__main__':
    write_fillable_pdf(INVOICE_TEMPLATE_PATH, INVOICE_OUTPUT_PATH, data_dict)
